<?php
	session_start();
	if(!($_SESSION["username"])||($_SESSION["role"]!="TUT")){
		//not logged in or tutor
		header("location:index.php");
	}
	
	// Connect to server and select databse.
	mysql_connect("localhost", "root", "")or die("cannot connect"); 
	mysql_select_db("Project 5.5")or die("cannot select DB");
	
	//gets message from url and displays
	function message(){
		if(isset($_GET['message'])){
			switch ($_GET['message']){
				case "delete":
					header("location:delete.php?file=".$_GET['file']."&referrer=classes");
					break;
				case "success":
					echo '<div id="error" style="display:block;background:#9F9;color:#777;"><a href="manageclasses.php">Data Imported</a></div>';
					break;
				case "import":	
					uploadFile();				
					break;
				case "export":
					echo '<div id="error" style="display:block;background:#9F9;color:#777;"><a href="manageclasses.php">Data Exported</a></div>';
					header('refresh:1;url=classesexport.php');
					break;
				case "edit":
					echo '<div id="error" style="display:block;background:#9F9;color:#777;"><a href="manageclasses.php">Class Edited</a></div>';
					break;
				case "add":
					echo '<div id="error" style="display:block;background:#9F9;color:#777;"><a href="manageclasses.php">Class Added</a></div>';
					break;
				case "remove":
					echo '<div id="error" style="display:block;background:#9F9;color:#777;"><a href="manageclasses.php">Class Removed</a></div>';
					break;
				}
		}
		
		
	}
	
	//uploads the user selected file
	function uploadFile(){
		
		$dir = 'temp/';
		$dh = opendir($dir);
		while (($file = readdir($dh)) != false){
			if (!is_dir($dir.$file)) {
				$filelist[] = $file;
			}
		}

		if(isset($filelist)){
			header("location:classimport.php?file=".$filelist[0]);
		}
	}
	
	//exports classes information into table
	function showClasses(){
		
		$sql='SELECT * FROM classes';
		$result=mysql_query($sql);
		$count=mysql_num_rows($result);
		
		
		echo '<tr style="background:#BBB;"><td>Class ID</td><td>Tutor</td><td>Location</td><td>Size</td><td>Day</td><td>Time</td><td></td><td></td><td></td></tr>';
		while ($row = mysql_fetch_row($result)){
			$sql='SELECT First_Name, Last_Name FROM users WHERE User_ID="'.$row[1].'"';
			$result2=mysql_fetch_row(mysql_query($sql));
			echo '<tr>';
			echo '<td>'.$row[0].'</td>';
			echo '<td>'.$result2[0].' '.$result2[1].'</td>';
			echo '<td>'.$row[2].'</td>';
			echo '<td>'.$row[3].'</td>';
			echo '<td>'.$row[4].'</td>';
			echo '<td>'.$row[5].'</td>';
			echo '<td><a href="editclass.php?id='.$row[0].'">Edit</a></td>';
			echo '<td><a href="removeclass.php?id='.$row[0].'">Remove</a></td>';
			echo '<td><a href="classexport.php?classid='.$row[0].'">Export</a></td>';
			echo '</tr>';
		}
		
		$result=mysql_query($sql);
	}
	
	//exports student information into dropdown
	function listStudents(){
		
		$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
		$objDB = mysql_select_db("Project 5.5");
		
		$sql="SELECT u.User_ID, u.First_Name, u.Last_Name FROM users u, studentinfo s WHERE s.Class_ID='0' AND u.User_ID=s.User_ID";
		$result= mysql_query($sql);
		
		while ($row = mysql_fetch_row($result)){			
			echo "<OPTION value='".$row[0]."'>".$row[1]." ".$row[2]."</OPTION>\n";
		}			
		
	}
	
	//exports tutor information into dropdown
	function listTutors(){
				
		$sql="SELECT User_ID, First_Name, Last_Name FROM users WHERE Role='TUT'";
		$result= mysql_query($sql);
		
		while ($row = mysql_fetch_row($result)){			
			echo "<OPTION value='".$row[0]."'>".$row[1]." ".$row[2]."</OPTION>\n";
		}			
		
	}
	
	//Inserts new class info
	if (isset($_POST['location'])) {
		
		$tutor=$_POST['tutor']; 
		$location=$_POST['location']; 
		$time=$_POST['time'];
		$weekday=$_POST['weekday'];
		$students=$_POST['classStudents'];
		$size = count($students);
		
		$sql="INSERT INTO classes (Tutor,Location,Size,Day,Time) VALUES ('".$tutor."','".$location."','".$size."','".$weekday."','".$time."')";
		$result=mysql_query($sql);
		
		foreach ($students as $s) {
			$sql="SELECT Class_ID FROM classes ORDER BY Class_ID DESC";
			$result=mysql_fetch_row(mysql_query($sql));
			
			$sql="UPDATE studentinfo SET Class_ID='".$result[0]."' WHERE User_ID='".$s."'";
			$result=mysql_query($sql);
			
		}
		
		header("location:manageclasses.php?message=add");
		
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<title>Class Management</title>		
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link href="css/main.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="js/formsValidate.js"/></script>
	</head>
	<body>
		<div id="topbar">
			<div id="qut_logo" />  
			<img src="../img/strapline.png" alt="Slogan" id="slogan"/>
		</div>
		<div id="menubar">
			<div id="breadcrumbs">
				<a href="index.php">Home</a> \ <a href="management.php">Management</a> \ <a href="manageclasses.php">Classes</a>
			</div>
			<div id="logout"> 
				<a href="login.php?destroy=true">Log Out</a>
			</div>
		</div>
		
		<?php
			include 'upload.php';
			message();
		?>
		<a href="manageclasses.php">
			<div id="error">
			</div>
		</a>
		<div style="" id="namebar_manageclass" ></div>
		<div id="body">
		<br/><br/>
			<table align="center" style="width:100%">
				<tr>
					<td style="vertical-align:top;width:50%">
						<div style=" text-align:center; background-color: #CCC;font-size:25pt;font-weight:bold;">Classes Excel Data</div>
						<table border="0" align="center" style="background:#FFF;width:100%">
							<tr>								
								<td scope="col" style="padding-left:40px;">	
									<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" name="submitform" enctype="multipart/form-data">
										<table cellpadding="0" cellspacing="0" border='0' width="100%"><tr><td>
											<a href="manageclasses.php?message=export"><input class="exportBtn" type="button" value=""/></a>
										</td><td>
											<input class="form-login" type="file" name="file" accept="application/vnd.ms-excel" style="position:relative;top:5px;"/>
											<input class="impBtn" type="submit" value="" style="position:relative;top:-10px;"/>										
										<input type='hidden' name='MAX_FILE_SIZE' value='2000000'>
										</td></tr></table>
									</form>
									<br/>
								</td>								
							</tr>
						</table>
						<br>
						<table align="center" style="width:100%">
							<tr>
								<div style="background-color: #CCC;font-size:25pt;font-weight:bold;text-align:center;">Add New Class</div>
								<td align="left" style="background:#fff">									
									<form  method="post" action="manageclasses.php" name="addclassform" onSubmit="return validateForm('addclassform');" style="padding-left:100px">
										Tutor:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
										<SELECT name="tutor" style="width:155px;">
											<option value="blank"></option>
											<?php
												listTutors();
											?>
										</SELECT>
										&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Students:
										<SELECT multiple="multiple" style='float:right;margin-right:65px;position:relative;top:0px;width:170px;height:180px;' name="classStudents[]">
											<?php
												listStudents();
											?>
										</SELECT><br/>
										Location:<input type='text' name='location'/><br/>
										Day:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='weekday'/><br/>
										Time:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='time'/><br/>
										<br/><input class="CreateBtn" type='submit' value='' style="margin-left:180px;"/>
									</form>
								</td>
							</tr>
						</table>
					</td>					
					<td align="center" style="width:600px;vertical-align:top;">
						<div style="background-color: #CCC;font-size:25pt;font-weight:bold;">Edit Classes</div>
						<table border="1" style="width:100%;background:#fff">							
							<?php
								showClasses();
							?>
						</table>
					</td>
				</tr>
			</table>
		</div>

		<div id="bottombg">
			<p>
				<a style="color:#333" href="http://www.qut.edu.au/additional/privacy">Privacy</a> 
				|
				<a style="color:#333" href="http://www.qut.edu.au/additional/copyright">Copyright</a>
				|
				<a style="color:#333" href="http://www.qut.edu.au/additional/disclaimer">Disclaimer</a>
				|
				<a style="color:#333" href="http://www.qut.edu.au/additional/accessibility">Accessibility</a>
			</p>
		</div>
	</body>
</html>